{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Marble Diagrams with RxPY\n",
    "\n",
    "This is a fantastic feature to produce and visualize streams and to verify how various operators work on them.\n",
    "\n",
    "Have also a look at [rxmarbles](http://rxmarbles.com) for interactive visualisations.\n",
    "\n",
    "\n",
    "**ONE DASH IS <font size=\"40px\">100</font> MILLISECONDS**!\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "%run startup.py"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create Streams from Strings: **from_marbles**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "========== from_marbles ==========\n",
      "\n",
      "module rx.testing.marbles\n",
      "@extensionclassmethod(Observable, alias=\"from_string\")\n",
      "def from_marbles(cls, string, scheduler=None):\n",
      "    Convert a marble diagram string to an observable sequence, using\n",
      "    an optional scheduler to enumerate the events.\n",
      "\n",
      "    Special characters:\n",
      "    - = Timespan of 100 ms\n",
      "    x = on_error()\n",
      "    | = on_completed()\n",
      "\n",
      "    All other characters are treated as an on_next() event at the given\n",
      "    moment they are found on the string.\n",
      "\n",
      "    Examples:\n",
      "    1 - res = rx.Observable.from_string(\"1-2-3-|\")\n",
      "    2 - res = rx.Observable.from_string(\"1-(42)-3-|\")\n",
      "    3 - res = rx.Observable.from_string(\"1-2-3-x\", rx.Scheduler.timeout)\n",
      "\n",
      "    Keyword arguments:\n",
      "    string -- String with marble diagram\n",
      "    scheduler -- [Optional] Scheduler to run the the input sequence on.\n",
      "\n",
      "    Returns the observable sequence whose elements are pulled from the\n",
      "    given marble diagram string.\n",
      "--------------------------------------------------------------------------------\n",
      "val: 1, dt: 0.0117020606995\n",
      "val: 2, dt: 0.125038146973\n",
      "val: 3, dt: 0.234502077103\n"
     ]
    }
   ],
   "source": [
    "rst(O.from_marbles)\n",
    "ts = time.time()\n",
    "# producing a stream\n",
    "s = O.from_marbles('1-2-3|')\n",
    "# mapping into real time:\n",
    "s2 = s.to_blocking()\n",
    "# adding times\n",
    "s3 = s2.map(lambda x: 'val: %s, dt: %s' % (x, time.time()-ts))\n",
    "# subscribing to it:\n",
    "d = s3.subscribe(print)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Visualize Streams as Marble Strings: **to_marbles**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "========== to_marbles ==========\n",
      "\n",
      "module rx.testing.marbles\n",
      "@extensionmethod(BlockingObservable, alias=\"to_string\")  # noqa\n",
      "def to_marbles(self, scheduler=None):\n",
      "    Convert an observable sequence into a marble diagram string\n",
      "\n",
      "    Keyword arguments:\n",
      "    scheduler -- [Optional] The scheduler used to run the the input\n",
      "        sequence on.\n",
      "\n",
      "    Returns marble string.\n",
      "--------------------------------------------------------------------------------\n",
      "1-a-b-2c-3|\n"
     ]
    }
   ],
   "source": [
    "rst(rx.core.blockingobservable.BlockingObservable.to_marbles)\n",
    "s1 = O.from_marbles('1---2-3|')\n",
    "s2 = O.from_marbles('-a-b-c-|')\n",
    "print(s1.merge(s2).to_blocking().to_marbles())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
